<template>
  <div class="page-container">
    <el-card class="box-card">
      <span>注册</span>
      <el-form
        ref="ruleFormRef"
        style="max-width: 600px"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
      >
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="ruleForm.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-row :gutter="8">
            <!-- 输入框占 20 列（共24列） -->
            <el-col :span="12">
              <el-input v-model="ruleForm.mobile" placeholder="请输入手机号" />
            </el-col>
            <!-- 按钮占 4 列 -->
            <el-col :span="4">
              <el-button type="primary" @click="sendMobile(ruleForm.mobile)"
                >发送验证码</el-button
              >
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm.code" placeholder="请输入验证码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="ruleForm.confirmPassword" placeholder="确认密码" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            注册
          </el-button>
          <el-button @click="back">取消注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { userRegister, SendMobile } from "../api/api";
import { useRouter } from "vue-router";

const router = useRouter();

interface RuleForm {
  nickName: string;
  mobile: string;
  password: string;
  confirmPassword: string;
  email: string;
  code: string;
}

const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  nickName: "",
  mobile: "",
  password: "",
  confirmPassword: "",
  email: "",
  code: "",
});

const rules = reactive<FormRules<RuleForm>>({
  nickName: [{ required: true, message: "昵称不能为空", trigger: "blur" }],
  email: [
    { required: true, message: "邮箱不能为空", trigger: "blur" },
    {
      type: "email",
      message: "请输入正确的邮箱地址",
      trigger: ["blur", "change"],
    },
  ],
  mobile: [
    { required: true, message: "手机号不能为空", trigger: "blur" },
    {
      pattern:
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      message: "请输入正确的手机号",
      trigger: "blur",
    },
  ],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
  confirmPassword: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value !== ruleForm.password) {
          callback(new Error("两次输入的密码不一致"));
        } else {
          callback();
        }
      },
      trigger: "blur",
    },
  ],
  code: [{ required: true, message: "验证码不能为空", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      userRegister(ruleForm).then((res) => {
        if (res.data.code == 200) {
          router.push("/");
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

const back = () => {
  router.push("/");
};

const sendMobile = async (mobile: any) => {
  SendMobile(mobile).then((res) => {
    ElMessage.success(res.data.data);
  });
};
</script>

<style scoped>
.page-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa; /* 可选：加背景色区分 */
}
</style>
